﻿using BizControl.Library.BusinessObjects;
using Csla.Xaml;
using BizControl.Presentation.Infrastructure.Controls;
using Microsoft.Practices.Prism.Regions;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism;
using System;
using BizControl.Presentation.Infrastructure.Constants;
using System.Windows.Input;
using BizControl.Library.BusinessObjects.Categories;
using BizControl.Library.BusinessObjects.People;

namespace BizControl.Presentation.PeopleModule.ViewModels
{
  [Export]
  public class PeopleViewModel : ViewModel<PersonList>, INavigationAware, IRegionMemberLifetime, IPartImportsSatisfiedNotification
  {
    private readonly DelegateCommand _editPersonCommand;
    private readonly DelegateCommand _refreshListCommand;
    private readonly DelegateCommand _addPersonCommand;

    private IRegionManager _regionManager;

    [ImportingConstructor]
    public PeopleViewModel(IRegionManager regionManager)
    {
      _regionManager = regionManager;
      _editPersonCommand = new DelegateCommand(EditPerson);
      _refreshListCommand = new DelegateCommand(RefreshList);
      _addPersonCommand = new DelegateCommand(AddPerson);
    }

    PersonList GetList()
    {
      return PersonList.GetPersonList();
    }

    private void EditPerson()
    {
      if (null != SelectedPerson)
      {
        var uriQuery = new UriQuery();
        uriQuery.Add(NavigationParametersNames.IdName, SelectedPerson.Id.ToString());
        var uri = new Uri(ViewNames.PersonEditViewName + uriQuery.ToString(), UriKind.Relative);
        this._regionManager.RequestNavigate(ShellRegionNames.MainContentRegion, uri);
      }
    }

    private void AddPerson()
    {
      var uriQuery = new UriQuery();
      uriQuery.Add(NavigationParametersNames.NewPersonName, NavigationParametersNames.CreateNewPerson);
      var uri = new Uri(ViewNames.PersonEditViewName + uriQuery.ToString(), UriKind.Relative);
      this._regionManager.RequestNavigate(ShellRegionNames.MainContentRegion, uri);
    }

    #region Commands

    public ICommand EditPersonCommand
    {
      get { return this._editPersonCommand; }
    }

    public ICommand AddPersonCommand
    {
      get { return this._addPersonCommand; }
    }

    private void RefreshList()
    {
      DoRefresh(GetList);
    }

    public ICommand RefreshListCommand
    {
      get { return this._refreshListCommand; }
    }

    #endregion

    #region Properties

    public PersonInfo SelectedPerson { get; set; }

    #endregion

    #region INavigationAware Members

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
      return true;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
    }

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
      RefreshList();
    }

    #endregion

    #region IRegionMemberLifetime Members

    public bool KeepAlive
    {
      get { return true; }
    }

    #endregion

    #region IPartImportsSatisfiedNotification Members

    public void OnImportsSatisfied()
    {
    }

    #endregion
  }
}
